package edu.berkeley.cs160.innovationers.shoppersidekick;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.c2dm.C2DMessaging;

import edu.berkeley.cs160.innovationers.shoppersidekick.R;
import edu.berkeley.cs160.innovationers.shoppersidekick.R.id;
import edu.berkeley.cs160.innovationers.shoppersidekick.R.layout;
import edu.berkeley.cs160.innovationers.shoppersidekick.rpclib.Configuration;
import edu.berkeley.cs160.innovationers.shoppersidekick.rpclib.ShopperSidekickProtocol;
import edu.berkeley.cs160.innovationers.shoppersidekick.sync.JSONRPCMethod;
import edu.berkeley.cs160.innovationers.shoppersidekick.sync.RPCClient;
import edu.berkeley.cs160.innovationers.shoppersidekick.test.TestRPCActivity;

import android.view.LayoutInflater;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ListActivity;
import android.database.Cursor;
import android.widget.BaseAdapter;

public class Login extends Activity{
	
	/// Place any server information/constants here for now? ******
	String usernameSERVER = "guest";
	String passwordSERVER = "";
	String phonenumberSERVER = "1112223333";
	
	//************************************************************
	
	private TextView loginStatusTV;
	private EditText usernameET, passwordET;
	private String un, pw; //string of username and password
	private Button loginButton;
	private myApp appInfo;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		loginStatusTV = (TextView)findViewById(R.id.loginStatusTV);
		loginStatusTV.setText("Please Login with your Gmail account");
		
		usernameET = (EditText)findViewById(R.id.usernameET);
		passwordET = (EditText)findViewById(R.id.passwordET);
		
		loginButton = (Button)findViewById(R.id.loginButton);
		loginButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				un = usernameET.getText().toString();
				pw = passwordET.getText().toString();
				appInfo = (myApp) getApplication();
				
				//NADIR LOOK HERE!!
				//please try to verify the user name and password here
				if(un.equals("yorke")||un.equals("nadir")){
					appInfo.setUserName(un);
					if (un.equals("yorke")) {
						RegisterUser("yorkethegod@gmail.com");
						appInfo.setC2DMUser("yorkethegod@gmail.com");
					} else
					{
						RegisterUser("nadirabid@gmail.com");
						appInfo.setC2DMUser("nadirabid@gmail.com");
					}
					finish();
				}else{
					AlertDialog myAlert = new AlertDialog.Builder(Login.this)
					.setMessage("Oops, seems like either your user name or password is incorrect. Please try again.")
					.setTitle("LOGIN FAILED")
					.setCancelable(true)
					.setNeutralButton(android.R.string.ok,
							new DialogInterface.OnClickListener(){
								public void onClick(DialogInterface dialog, int whichButton){}
								})
					.show();  
					usernameET.setText("");
					passwordET.setText("");
					
				}			
			}
		});
		
		Button testRPCButton = (Button) findViewById(R.id.testRPCButton);
		testRPCButton.setOnClickListener( new OnClickListener() {

			@Override
			public void onClick(View v) {
	    		Intent intent = new Intent(Login.this, TestRPCActivity.class);
	    		startActivity(intent);
			}
			
		});
	}

	public void RegisterUser(String User) {
		
		Log.d(Config.SS_TAG, "RegisterUser button called.");

		try {
			JSONRPCMethod registerUser = new JSONRPCMethod.RegisterDeviceWithUser(User, C2DMessaging.getRegistrationId(this));
			
			JSONObject returnValue = RPCClient.execute(registerUser);
			
			String ret = returnValue.getString(ShopperSidekickProtocol.RegisterDeviceWithUser.RETURN[0]);
			
			Log.d(Config.SS_TAG, "Message Received: " + ret);
		} catch (JSONException e) {
			Log.e(Config.SS_TAG,"We got an error while making the object");
			e.printStackTrace();
		}
	}
	
}
